package de.hsowl.tod4a.global.listener;

import java.util.HashMap;

/**
 * Database listener. When a value changes onDataChanged will executed.s
 * 
 * @author Sebastian Witt
 * 
 */
public abstract class DataChangedListener {

	public static final int STORED = 1;
	public static final int REMOVED = 2;
	public static final int UPDATED = 3;

	public HashMap<String, Integer> classes = new HashMap<String, Integer>();

	public DataChangedListener(Class<?>... classes) {
		initialize(classes);
	}

	public DataChangedListener(Class<?> clazz) {
		initialize(clazz);
	}

	/**
	 * Adds a class to be listened
	 * 
	 * @param classes
	 */
	private void initialize(Class<?>... classes) {
		if (classes != null && classes.length > 0) {
			for (Class<?> clazz : classes) {
				if (clazz != null && this.classes.get(clazz.getName()) == null) {
					this.classes.put(clazz.getName(), 1);
				}
			}
		} else {
			throw new NullPointerException("At least one calss is required");
		}
	}

	/**
	 * This methos is called whenever a watched class value is changed
	 * 
	 * @param clazz
	 */
	public abstract void onDataChanged(String clazz, int operation);
}
